<template>
	<view :data-theme="theme">
		<!-- #ifndef APP-PLUS || MP -->
		<nav-bar :navTitle="navTitle"></nav-bar>
		<!-- #endif -->
		<view class="mp-bg">
			<scroll-view class="scroll-view_x" scroll-x style="white-space: nowrap; vertical-align: middle"
				show-scrollbar="false">
				<view class="nav borRadius14 acea-row">
					<text class="item" :class="orderStatus == 0 ? 'on' : ''" @click="statusClick(0)">
						<text>全部</text>
					</text>
					<text class="item" :class="orderStatus == 1 ? 'on' : ''" @click="statusClick(1)">
						<text>申请中</text>
					</text>
					<text class="item" :class="orderStatus == 2 ? 'on' : ''" @click="statusClick(2)">
						<text>待支付</text>
					</text>
					<text class="item" :class="orderStatus == 10 ? 'on' : ''" @click="statusClick(10)">
						<text>待收货</text>
					</text>
					<text class="item" :class="orderStatus == 4 ? 'on' : ''" @click="statusClick(4)">
						<text>租用中</text>
					</text>
					<text class="item" :class="orderStatus == 5 ? 'on' : ''" @click="statusClick(5)">
						<text>已取消</text>
					</text>
				</view>
			</scroll-view>
		</view>
		<view class="my-order">
			<view class="list">
				<view class="item" v-for="(item, index) in orderList" :key="index">
					<view class="title acea-row row-between-wrapper">
						<view class="acea-row row-middle">
							<view v-if="item.merName">
								<navigator :url="`/pages/merchant/home/index?id=${item.merId}`" hover-class="none">
									<text class="iconfont icon-shangjiadingdan mr10"></text>
									<text class="mr10">{{ item.merName }} -- {{ index}}</text>
									<text class="iconfont icon-xiangyou"></text>
								</navigator>
							</view>
							<view v-else>
								<text class="mr10">{{ item.orderNo }}</text>
							</view>
						</view>
						<view class="font_color" v-if="item.returnStatus == 2">归还中</view>
						<view class="font_color" v-else-if="item.status == 4 && item.isReletOrder == 1">续租中</view>
						<view class="font_color" v-else-if="item.status == 6 && item.returnStatus == 6">已归还</view>
						<view class="font_color" v-else>{{ item.status | orderStatusFilter }}</view>
					</view>
					<view @click="goOrderDetails(item)" class="item-info acea-row row-between row-top">
						<view class="pictrue">
							<easy-loadimage mode="widthFix" :image-src="item.goodsImage"
								v-if="item.goodsImage"></easy-loadimage>
							<image v-else
								src='https://img.youxuanan.com/crmebimage/public/product/2024/10/10/2553d153f4464ac3b012c5ef0f81b9781yy90rqes7.png'
								mode="widthFix"></image>
						</view>
						<view class="text">
							<view class="nameBox">
								<view class="line2 goodsName">{{ item.goodsName }}</view>
								<view class="sku line1">规格:{{ item.specName }}</view>
								<view class="totalPrice">每期租金:￥{{ item.periodicRent }}</view>
							</view>
						</view>
					</view>
					<view class="order">订单号:{{item.orderNo}}</view>
					<!-- 订单状态（0：待支付，1：待发货,2：部分发货， 3：待核销，4：待收货,5：已收货,6：已完成，9：已取消  -->
					<view class="bottom acea-row row-right row-middle">
						<view class="bnt bg_color" v-if="item.status == 1 && item.depositStatus == 0"
							@click="verifySystem(item.orderNo)">授权</view>
						<view class="bnt bg_color" v-if='!nickname' @click="toDetail">完善资料</view>
						<view class="bnt bg_color" v-if="item.status == 2" @click="toPayFor(item.orderNo)">去支付</view>
						<view class="bnt bg_color" v-if="item.status == 10" @click="findMyFast(item)">查看物流</view>
						<!-- -->
						<view class="bnt bg_color"
							v-if="((item.status == 4 && !item.returnStatus)) || (item.status == 5 && item.returnStatus == 1)"
							@click="returnBack(item)">归还</view><!--租用中 续租 待归还-->
						<!-- <view class="bnt bg_color" v-if="[4,5].includes(item.status) && (!item.returnStatus || item.returnStatus != 2 || item.isReletOrder == 1)" @click="returnBack(item)">归还</view> -->
						<view class="bnt bg_color"
							v-if="((item.status == 4 && !item.returnStatus)) || (item.status == 5 && item.returnStatus == 1) || item.status == 7"
							@click="toBuyOut(item)">买断</view><!--租用中 续租 待归还 已逾期-->
						<!-- <view class="bnt bg_color" v-if="[4,5,7].includes(item.status) && (!item.returnStatus || item.returnStatus != 2 || item.isReletOrder == 1)" @click="toBuyOut(item)">买断</view> -->
						<view class="bnt bg_color" v-if="item.status == 5 && item.returnStatus == 1"
							@click="tapContinue(item)">续租</view>
						<view class="bnt bg_color" v-if="(item.status == 4 && !item.returnStatus) || item.status == 7" @click="toPayFor(item.orderNo)">
							支付租金</view>
						<!-- <view class="bnt bg_color" v-if="[4,7].includes(item.status) && (!item.returnStatus || item.returnStatus != 2 || item.isReletOrder == 1)" @click="toPayFor(item.orderNo)">支付租金</view> -->
						<view class="bnt bg_color"
							v-if="[2,6].includes(item.returnStatus)"
							@click="backInfo(item)">归还详情</view>
						<view class="bnt bg_color" v-if="[6,8,12].includes(item.status) || item.returnStatus == 6"
							@click="toPathProduct(item)">再次下单</view>
						<view class="bnt cancelBnt" v-if="item.status == '1' && item.cancelStatus == 1">取消中</view>
						<view class="bnt cancelBnt" v-if="item.status == '1' && item.cancelStatus != 1"
							@click="cancelOrder(index, item.orderNo)">
							取消订单</view>
						<!-- <view class="bnt bg_color" v-if="item.status == '2'" @click="goPay(item)">立即付款</view>
						<view class="bnt bg_color" v-if="item.status == '3'">查看物流</view> -->
						<!-- <view class="bnt bg_color" v-if="item.paid || item.status == 9" @click="goOrderDetails(item)">查看详情</view> -->
						<!-- <view class="bnt cancelBnt" v-if="item.status == 6 || item.status == 9" @click="delOrder(item.orderNo, index)">删除订单</view> -->
					</view>
				</view>
			</view>

			<view class="loadingicon acea-row row-center-wrapper">
				<text class="loading iconfont icon-jiazai" :hidden="loading == false"></text>
				{{ orderList.length > 0 ? loadTitle : '' }}
			</view>
			<view class="noCart" v-if="orderList.length == 0 && isShow && !loading">
				<view class="pictrue">
					<image :src="urlDomain + 'crmebimage/presets/nodingdan.png'"></image>
					<view class="default_txt">暂无订单信息~</view>
				</view>
			</view>
		</view>

		<view class="uniappBox">
			<view :hidden="!uniappShowModel" class="uniappContant">
				<view style="padding: 0 150rpx;">
					<view class="uniappTitle">取消订单</view>
					<text style="color: #C0C4CC;">取消原因</text>
					<view class="uniappNotice">
						<radio-group @change="radioChange">
							<label class="uni-list-cell uni-list-cell-pd" v-for="(item, index) in items"
								:key="item.value" style="width: 100%;text-align: center;">
								<view>
									<radio :value="item.value" :checked="index === current" />
									<view
										style="display: inline-block;width: 100px;margin-left: 15px;text-align: left;">
										{{item.name}}
									</view>
								</view>

							</label>
						</radio-group>
					</view>
				</view>
				<!-- 确定取消按钮 -->
				<view class="uniappBtn">
					<view @tap="uniappShowModel = false">再考虑下</view>
					<view @tap="confirm()">取消订单</view>
				</view>
			</view>
			<!-- 背景黑色蒙版 -->
			<view class="uniappBgdCol" :hidden="!uniappShowModel" @tap="uniappShowModel = false"></view>
		</view>
	</view>
</template>

<script>
	import {
		Debounce
	} from '@/utils/validate.js';
	import {
		getOrderList,
		closeMyOrder,
		orderDel,
		EFaceVerifyOrder,
	} from '@/api/order.js';
	import {
		userCenterInfo
	} from '@/api/user.js';
	import {
		openOrderSubscribe
	} from '@/utils/SubscribeMessage.js';
	import navBar from '@/components/navBar';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from 'vuex';
	import {
		setThemeColor
	} from '@/utils/setTheme.js';
	import animationType from '@/utils/animationType.js';
	import easyLoadimage from '@/components/base/easy-loadimage.vue';
	const app = getApp();
	export default {
		components: {
			navBar,
			easyLoadimage
		},
		data() {
			return {
				uniappShowModel: false, //默认不显示
				urlDomain: this.$Cache.get('imgHost'),
				orderData: {},
				navTitle: '我的订单',
				loading: false, //是否加载中
				loadend: false, //是否加载完毕
				loadTitle: '加载更多', //提示语
				orderList: [], //订单数组
				orderStatus: 0, //订单状态
				page: 1,
				limit: 20,
				pay_order_id: '',
				totalPrice: '0',
				isShow: false,
				theme: app.globalData.theme,
				bgColor: '#e93323',
				nickname: '',
				items: [{
						value: '想要重新下单',
						name: '想要重新下单',
					},
					{
						value: '商品价格较贵',
						name: '商品价格较贵'
					},
					{
						value: '等待时间长',
						name: '等待时间长'
					},
					{
						value: '是想了解流程',
						name: '是想了解流程'
					},
					{
						value: '不想要了',
						name: '不想要了'
					}
				],
				reason: {
					orderNo: '',
					reason: ''
				}
			};
		},
		computed: mapGetters(['isLogin', 'userInfo']),
		// 滚动监听
		onPageScroll(e) {
			// 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件 
			uni.$emit('scroll');
		},
		onShow() {
			let that = this;
			that.bgColor = setThemeColor();
			uni.setNavigationBarColor({
				frontColor: '#ffffff',
				backgroundColor: that.bgColor
			});
			if (this.isLogin) {
				this.loadend = false;
				this.page = 1;
				this.$set(this, 'orderList', []);
				this.getOrderList();
				this.getUserCenterInfo();
			} else {
				toLogin();
			}
		},
		methods: {
			//去支付
			toPayFor(orderNo) {
				uni.navigateTo({
					url: `/pages/goods/order_details/payInfo?orderNo=${orderNo}&isBuyout=${0}&renewal=${0}`
				});
			},
			//去买断
			toBuyOut(row) {
				uni.navigateTo({
					url: `/pages/goods/order_details/payInfo?orderNo=${row.orderNo}&isBuyout=${1}&renewal=${0}`
				});
			},
			//去完善资料
			toDetail() {
				uni.navigateTo({
					url: `/pages/users/uploadUserInfoAli/index`
				});
			},
			//查询物流
			findMyFast(row) {
				uni.navigateTo({
					url: `/pages/other/logistics?tradeNo=${row.tradeNo}&logisticsCode=${row.expressCode}&waybillNo=${row.trackingNumber}`
				});
			},
			//去续租
			tapContinue(row) {
				uni.navigateTo({
					url: `/pages/goods/order_details/payInfo?orderNo=${row.orderNo}&isBuyout=${0}&renewal=1`
				});
			},
			//去归还
			returnBack(row) {
				//逾期状态不允许归还
				if (row.status == 7) {
					return this.$util.Tips({
						title: '逾期账单不允许归还'
					});
				}
				console.log(row)
				const detail = {
					serialNumber: row.serialNumber,
					trackingNumber: row.trackingNumber,
					goodsImage: row.goodsImage,
					goodsName: row.goodsName,
					specName: row.specName,
					merId: row.merId
				}
				uni.navigateTo({
					url: `/pages/goods/give_back/index?orderNo=${row.orderNo}&detail=` + JSON.stringify(detail)
				});
			},
			//归还详情
			backInfo(row) {
				const detail = {
					goodsImage: row.goodsImage,
					goodsName: row.goodsName,
					specName: row.specName
				}
				uni.navigateTo({
					url: `/pages/goods/give_back/details?orderNo=${row.orderNo}&detail=` + JSON.stringify(detail)
				});
			},
			//跳转商品详情页
			toPathProduct(row) {
				uni.navigateTo({
					url: '/pages/goods/goods_details/index?id=' + row.goodsId + '&merId=' + row.merId
				})
			},
			/**
			 * 生命周期函数--监听页面加载
			 */
			onLoad: function(options) {
				if (options.status) this.orderStatus = options.status;
			},
			/**
			 * 取消订单
			 *
			 */
			radioChange: function(evt) {
				this.reason.reason = evt.detail.value;
			},
			cancelOrder: function(index, orderNo) {
				let that = this;
				if (!orderNo)
					return that.$util.Tips({
						title: '缺少订单号无法取消订单'
					});
				this.reason.orderNo = orderNo;
				this.uniappShowModel = true;
			},
			/**
			 * 打开支付组件
			 *
			 */
			goPay: Debounce(function(item) {
				uni.showLoading({
					title: '加载中...'
				});

				uni.navigateTo({
					url: `/pages/goods/order_payment/index?orderNo=${item.orderNo}&payPrice=${item.payPrice}`
				});
			}),
			/**
			 * 去订单详情
			 */
			goOrderDetails: function(item, status) {

				if (!item.orderNo)
					return this.$util.Tips({
						title: '缺少订单号无法查看订单详情'
					});
				//虚拟商品
				if (item.type === 5 || item.type === 6) {
					// #ifdef MP
					uni.navigateTo({
						url: '/pages/goods/virtual_order_details/index?orderNo=' + item.orderNo
					});
					// #endif
					// #ifndef MP
					uni.navigateTo({
						animationType: animationType.type,
						animationDuration: animationType.duration,
						url: '/pages/goods/virtual_order_details/index?orderNo=' + item.orderNo
					});
					// #endif
				} else {
					// #ifdef MP
					uni.navigateTo({
						url: '/pages/goods/order_details/AliIndex?orderNo=' + item.orderNo +
							'&depositStatus=' + item.depositStatus + '&id=' + item.id + '&goodsId=' + item
							.goodsId + '&merId=' + item.merId + '&isReletOrder=' + item.isReletOrder +
							'&returnStatus=' + item.returnStatus
					});
					// #endif
					// #ifndef MP
					uni.navigateTo({
						animationType: animationType.type,
						animationDuration: animationType.duration,
						url: '/pages/goods/order_details/index?orderNo=' + item.orderNo
					});
					// #endif
				}
			},
			/**
			 * 切换类型
			 */
			statusClick: function(status) {
				if (status == this.orderStatus) return;
				this.orderStatus = status;
				this.loadend = false;
				this.page = 1;
				this.$set(this, 'orderList', []);
				this.getOrderList();
			},
			/**
			 * 获取订单列表
			 */
			getOrderList: function() {
				let that = this;
				if (that.loadend) return;
				if (that.loading) return;
				that.loading = true;
				that.loadTitle = '加载更多';
				getOrderList(that.orderStatus)
					.then((res) => {
						console.log(res, 123)
						let list = res.data || [];
						let loadend = list.length < that.limit;
						that.orderList = that.$util.SplitArray(list, that.orderList);
						that.$set(that, 'orderList', that.orderList);
						that.loadend = loadend;
						that.loading = false;
						that.loadTitle = loadend ? '我也是有底线的' : '加载更多';
						that.page = that.page + 1;
						that.isShow = true;
					})
					.catch((err) => {
						that.loading = false;
						that.loadTitle = '加载更多';
					});
			},

			//是否完善资料
			getUserCenterInfo: function() {
				//if (this.centerMenu.length) return;
				userCenterInfo().then(res => {
					this.nickname = res.data.realName;
				}).catch(err => {

				});
			},
			// 点击确定按钮模态框消失
			confirm() {
				if (!this.reason.reason) {
					this.$util.Tips({
						title: '请选择一个取消原因'
					});
					return;
				}
				closeMyOrder(this.reason.orderNo, this.reason.reason)
					.then((res) => {
						this.uniappShowModel = false;
						this.getOrderList();
						this.$util.Tips({
							title: '取消成功',
							icon: 'success'
						});
					})
					.catch((err) => {
						return this.$util.Tips({
							title: err
						});
					});
			},

			/**
			 * 删除订单
			 */
			delOrder: function(orderNo, index) {
				uni.showModal({
					content: '确定删除该订单',
					cancelText: '取消',
					confirmText: '确定',
					showCancel: true,
					confirmColor: '#f55850',
					success: (res) => {
						if (res.confirm) {
							let that = this;
							orderDel(orderNo)
								.then((res) => {
									that.orderList.splice(index, 1);
									that.$set(that, 'orderList', that.orderList);
									that.$set(that.orderData, 'unpaid_count', that.orderData
										.unpaid_count - 1);
									that.getOrderData();
									return that.$util.Tips({
										title: '删除成功',
										icon: 'success'
									});
								})
								.catch((err) => {
									return that.$util.Tips({
										title: err
									});
								});
						} else {}
					}
				});
			},
			//去授权
			verifySystem(orderNo) {
				const that = this;
				EFaceVerifyOrder(orderNo)
					.then(res => {
						if (res.code == 200) {
							my.tradePay({
								orderStr: res.data
									.orderStr, // 完整的支付参数拼接成的字符串，从 alipay.fund.auth.order.app.freeze 接口获取
								success: (res) => {
									console.log(res, '成功回调')
									if (rusult.resultCode == 9000) {
										uni.redirectTo({
											url: '/pages/other/orderSuccess'
										})
									} else {
										uni.showToast({
											title: '免押失败',
											icon: 'none'
										})
									}
								},
								fail: (res) => {
									my.alert({
										content: JSON.stringify(res),
									});
								}
							});
						} else {
							my.alert({
								content: res.message + '1'
							});
						}
					})
					.catch(err => {
						my.alert({
							content: err + '2'
						});
					})
			}
		},
		onReachBottom: function() {
			//this.getOrderList();
		}
	};
</script>
s
<style scoped lang="scss">
	.activity {
		width: 60rpx;
		height: 30rpx;
		border-radius: 4px 4px 4px 4px;
		opacity: 1;
		display: inline-block;
		color: #fff;
		margin-right: 8rpx;
		line-height: 30rpx;
		text-align: center;
		font-size: 20rpx;
	}

	.mp-bg {
		top: 0;
		position: fixed;
		left: 0;
		/* #ifdef H5 */
		top: 74rpx;
		/* #endif */
		width: 100%;
		height: 120rpx;
		@include index-gradient(theme);
		padding: 30rpx 30rpx 0 30rpx;
		z-index: 99;
	}

	.my-order {
		/* #ifdef H5 */
		margin-top: 110rpx !important;
		/* #endif */
		/* #ifdef MP || APP-PLUS */
		margin-top: 140rpx !important;

		/* #endif */
		.header {
			height: 250rpx;
			padding: 0 30rpx;
		}
	}

	.bg_color {
		//@include main_bg_color(theme);
		background-color: rgba(255, 110, 19, 1);
	}

	.my-order .header .picTxt {
		height: 190rpx;
	}

	.my-order .header .picTxt .text {
		color: rgba(255, 255, 255, 0.8);
		font-size: 26rpx;
		font-family: 'Guildford Pro';
	}

	.my-order .header .picTxt .text .name {
		font-size: 34rpx;
		font-weight: bold;
		color: #fff;
		margin-bottom: 20rpx;
	}

	.my-order .header .picTxt .pictrue {
		width: 120rpx;
		height: 120rpx;
	}

	.my-order .header .picTxt .pictrue image {
		width: 100%;
		height: 100%;
	}

	.nav {
		background-color: #fff;
		padding: 0 50rpx;
		justify-content: space-between;
	}

	.nav .item {
		font-size: 26rpx;
		color: #282828;
		padding: 25rpx 0;
		display: inline-block;
	}

	.nav .item.on {
		/* #ifdef H5 || MP */
		font-weight: bold;
		/* #endif */
		/* #ifdef APP-PLUS */
		color: #000;
		/* #endif */
		position: relative;
		@include tab_border_bottom(theme);
	}

	.nav .item .num {
		margin-top: 18rpx;
	}

	.my-order .list {
		width: 690rpx;
		margin: 14rpx auto 0 auto;
	}

	.my-order .list .item {
		background-color: #fff;
		border-radius: 14rpx;
		margin-bottom: 14rpx;
	}

	.my-order .list .item .title {
		height: 84rpx;
		padding: 0 24rpx;
		border-bottom: 1rpx solid #eee;
		font-size: 28rpx;
		color: #282828;
	}

	.my-order .list .item .title .sign {
		font-size: 24rpx;
		padding: 0 13rpx;
		height: 36rpx;
		margin-right: 15rpx;
		border-radius: 18rpx;
		@include coupons_border_color(theme);
		@include main_color(theme);
	}

	.my-order .list .item .item-info {
		padding: 0 30rpx;
		margin-top: 22rpx;
	}

	.my-order .list .item .item-info .pictrue {
		width: 120rpx;
		height: 120rpx;
	}

	.my-order .list .item .item-info .pictrue image {
		width: 100%;
		height: 100%;
		border-radius: 14rpx;
		overflow: hidden;
	}

	.my-order .list .item .item-info {
		.nameBox {
			position: relative;
			height: 120rpx;
		}

		.sku {
			font-size: 30rpx;
			margin: 10px 0;
		}

		.text {
			width: 496rpx;
			font-size: 28rpx;
			color: #999;

			padding-left: 15px;
		}
	}

	.my-order .list .item .item-info .text .name {
		width: 326rpx;
		color: #282828;
	}

	.my-order .list .item .item-info .text .money {
		text-align: right;
	}

	.font_color {
		//@include main_color(theme);
		color: rgba(255, 110, 19, 1);
	}

	.my-order .list .item .totalPrice {
		font-size: 26rpx;
		color: #282828;
	}

	.my-order .list .item .totalPrice .money {
		font-size: 28rpx;
		font-weight: bold;
		@include price_color(theme);
	}

	.my-order .list .item .bottom {
		height: 107rpx;
		padding: 0 30rpx;
	}

	.my-order .list .item .bottom .bnt {
		width: 22%;
		height: 60rpx;
		text-align: center;
		line-height: 60rpx;
		color: #fff;
		border-radius: 50rpx;
		font-size: 27rpx;
	}

	.my-order .list .item .bottom .bnt.cancelBnt {
		border: 1rpx solid #ddd;
		color: #aaa;
	}

	.my-order .list .item .bottom .bnt~.bnt {
		margin-left: 17rpx;
	}

	.noCart {
		margin-top: 171rpx;
		padding-top: 0.1rpx;
	}

	.noCart .pictrue {
		width: 414rpx;
		height: 336rpx;
		margin: 78rpx auto 56rpx auto;
	}

	.noCart .pictrue image {
		width: 100%;
		height: 100%;
	}

	.icon-xiangyou {
		font-size: 24rpx;
	}

	.default_txt {
		font-size: 26rpx;
		color: #999;
		text-align: center;
	}

	.order {
		padding: 25px 30px;
	}

	.goodsName {
		width: 90%;
		color: black;
		font-weight: bold;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.uniappBgdCol {
		position: fixed;
		top: 0%;
		left: 0%;
		width: 100%;
		height: 100%;
		background-color: black;
		z-index: 99;
		-moz-opacity: 0.6;
		opacity: .6;
		filter: alpha(opacity=88);
	}

	.uniappContant {
		position: fixed;
		top: 20%;
		left: 50%;
		width: 80%;
		margin-left: -40%;
		background-color: white;
		z-index: 100;
		border-radius: 10rpx;
	}

	.uniappTitle {
		display: flex;
		justify-content: center;
		font-size: 32rpx;
		font-weight: bold;
		color: #161303;
		font-size: 28rpx;
		padding: 20rpx 0rpx;
	}

	.uniappNotice {
		font-size: 28rpx;
		color: #121212;
		display: flex;
		justify-content: center;
		padding: 20rpx 0 40rpx 0rpx;
	}

	.uniappBtn {
		display: flex;
		justify-content: space-between;
	}

	.uniappBtn view {
		width: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 4% 0%;
		font-size: 28rpx;
		border-top: 1px solid #E4E7ED;
	}

	.uniappBtn view:first-child {
		border-right: 1px solid #E4E7ED;
	}

	.uniappBtn view:last-child {
		color: rgba(255, 110, 19, 1);
	}
	.uni-list-cell-pd{
		margin: 3px 0;
	}
</style>

